package cn.bin758.h5game.module.game.service.GameData;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.bin758.h5game.module.game.controller.admin.GameData.vo.*;
import cn.bin758.h5game.module.game.dal.dataobject.GameData.GameDataDO;
import cn.bin758.h5game.framework.common.pojo.PageResult;

import cn.bin758.h5game.module.game.convert.GameData.GameDataConvert;
import cn.bin758.h5game.module.game.dal.mysql.GameData.GameDataMapper;

import static cn.bin758.h5game.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.bin758.h5game.module.game.enums.ErrorCodeConstants.*;

/**
 * 游戏数据 Service 实现类
 *
 * @author bin758
 */
@Service
@Validated
public class GameDataServiceImpl implements GameDataService {

    @Resource
    private GameDataMapper dataMapper;

    @Override
    public String createData(GameDataCreateReqVO createReqVO) {
        // 插入
        GameDataDO data = GameDataConvert.INSTANCE.convert(createReqVO);
        dataMapper.insert(data);
        // 返回
        return data.getId();
    }

    @Override
    public void updateData(GameDataUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateDataExists(updateReqVO.getId());
        // 更新
        GameDataDO updateObj = GameDataConvert.INSTANCE.convert(updateReqVO);
        dataMapper.updateById(updateObj);
    }

    @Override
    public void deleteData(String id) {
        // 校验存在
        this.validateDataExists(id);
        // 删除
        dataMapper.deleteById(id);
    }

    private void validateDataExists(String id) {
        if (dataMapper.selectById(id) == null) {
            throw exception(DATA_NOT_EXISTS);
        }
    }

    @Override
    public GameDataDO getData(String id) {
        return dataMapper.selectById(id);
    }

    @Override
    public List<GameDataDO> getDataList(Collection<String> ids) {
        return dataMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GameDataDO> getDataPage(GameDataPageReqVO pageReqVO) {
        return dataMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GameDataDO> getDataList(GameDataExportReqVO exportReqVO) {
        return dataMapper.selectList(exportReqVO);
    }

}
